package com.ctshk.app.order.goods.controller;

import com.alibaba.fastjson.JSONObject;
import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.order.goods.dto.CurrentUsreCartInfoDTO;
import com.ctshk.rpc.order.goods.req.ShoppingCartAddReq;
import com.ctshk.rpc.order.goods.service.IShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author 姜斌文
 * @since 2021-04-19
 */
@Api(value="商城C端购物车", tags="商城C端购物车")
@RestController
@RequestMapping("/shopping/cart")
public class OrderShoppingCartController extends BaseController {

    @DubboReference
    private IShoppingCartService shoppingCartService;

    @ApiOperation(value = "添加商品到购物车", notes = "添加商品到购物车")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "添加商品到购物车", operDesc = "添加商品到购物车")
    @PostMapping("/addShoppingCart")
    public Result addShoppingCart(@RequestBody @Valid ShoppingCartAddReq obj) throws Exception {
        shoppingCartService.addShoppingCart(obj, getCurrentUser(getRequest()).getId());
        return Result.success();
    }


    @ApiOperation(value = "刪除购物车商品", notes = "刪除购物车商品")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.DELETE, operTitle = "刪除购物车商品", operDesc = "刪除购物车商品")
    @PostMapping("/delShoppingCart")
    public Result delShoppingCart(@RequestBody @Valid List<Long> ids) throws Exception {
        shoppingCartService.delAppShoppingCart(ids, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value = "购物车商品增減購買數量", notes = "购物车商品增減購買數量")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "购物车商品增減購買數量", operDesc = "购物车商品增減購買數量")
    @PostMapping("/updateShoppingCartNumber")
    public Result updateShoppingCartNumber(@RequestBody @Valid JSONObject req) throws Exception {
        shoppingCartService.updateShoppingCartNumber(req, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value = "獲取當前用戶購物車商品列表", notes = "獲取當前用戶購物車商品列表")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "獲取當前用戶購物車商品列表", operDesc = "獲取當前用戶購物車商品列表")
    @GetMapping("/findCurrentUsreCartInfoList")
    public Result<List<CurrentUsreCartInfoDTO>> findCurrentUsreCartInfoList() throws Exception {
        List<CurrentUsreCartInfoDTO> cartInfoDTOs= shoppingCartService.findCurrentUsreCartInfoList(getCurrentUser(getRequest()).getId());
        return Result.success(cartInfoDTOs);
    }
}
